package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.entity.*;
import com.itheima.mapper.SpecMapper;
import com.itheima.pojo.Spec;
import com.itheima.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper,Spec> implements SpecService {

    @Autowired
    private SpecMapper specMapper;
    @Override
    public Result<Spec> findPage(Integer curPage, Integer size) {
        IPage<Spec> specPage = new Page<>(curPage,size);
        LambdaQueryWrapper<Spec> wrapper = Wrappers.lambdaQuery(Spec.class);
        specMapper.selectPage(specPage,wrapper);
        List<Spec> specs=specPage.getRecords();
        if(specs.size()==0){
            return new Result(false, StatusCode.ERROR,"查询失败",null);
        }
        PageBean<Spec> pageBean= new ConverterUtil<Spec>().convertFromPageInfoToPageBean(specPage);
        return new Result(true,StatusCode.OK,"查询成功",pageBean);

    }

    @Override
    @Transactional
    public Result add(Spec spec) {
        int insert = specMapper.insert(spec);
        if (insert!=1){
            return new Result(false, StatusCode.ERROR,"添加失败",null);
        }
        return new Result(true, StatusCode.OK,"添加成功",insert);
    }

    @Override
    public Result<Spec> findById(Integer id) {
        Spec spec = specMapper.selectById(id);
        if (spec==null){
            return new Result(false, StatusCode.ERROR,"查询失败",null);
        }
        return new Result(true, StatusCode.OK,"查询成功",spec);
    }

    @Override
    public Result updateSpec(Spec spec) {
        int update = specMapper.updateById(spec);
        if(update!=1){
            return new Result(false, StatusCode.ERROR,"修改失败",null);
        }
        return new Result(true, StatusCode.OK,"修改成功",update);
    }

    @Override
    public Result deleteById(Integer id) {
        int delete = specMapper.deleteById(id);
        if (delete!=1){
            return new Result(false, StatusCode.ERROR,"删除失败",null);
        }
        return new Result(true, StatusCode.OK,"删除成功",delete);
    }


}
